import requests
import urllib.request, urllib.error, urllib.parse
import http.cookiejar

# from ...core.config.global_var import Variables
from ...core.exception.exception_go import *

from CoTec.core.exception.exception_go import ExceptionGo as E
from CoTec.utility.file.file_go import FileHelper as file
from CoTec.utility.string.string_go import StringHelper as string
# from CoTec.core.request.proxy_go import ProxyHelper

from CoTec.utility.spider.spider import spider

class RequestHelper:
    session = requests.Session()

    proxy_http = None
    proxy_https = None
    proxies = dict()

    config = None

    error_result = ['{"success":true,"value":-999}']
    codes = ['gb2312', 'gbk', 'utf-8']

    @staticmethod
    def init(conf):
        RequestHelper.config = conf

    @staticmethod
    def __to_encode(req, **pars):
        html_doc = req.text

        incode = spider.get_txt_page_encode(html_doc)

        if incode is not None and incode != '':
            encode = incode.lower()
            if encode in RequestHelper.codes:
                try:
                    html_doc = str(req.content, encode)
                except UnicodeDecodeError as e:
                    if req.encoding == 'ISO-8859-1':
                        encoding = None
                        encodings = requests.utils.get_encodings_from_content(req.text)
                        if encodings:
                            encoding = encodings[0]
                        else:
                            encoding = req.apparent_encoding
                        encode_content = req.content.decode(encoding, 'replace').encode('utf-8', 'replace')
                        html_doc = str(encode_content, 'utf-8')

        #过滤html标签
        html_doc = string.html_tag_killer(html_doc)

        return html_doc

    @staticmethod
    def __in__get(url, **pars):

        header = \
            {
                "User-Agent":
                    # "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36"
                    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10B329 MicroMessenger/6.5.7"
            }

        req = RequestHelper.session.get(url, headers=header)

        return RequestHelper.__to_encode(req, **pars)

    @staticmethod
    def __in_get(url, ** pars):

        header = \
            {
                "User-Agent":
                    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36"
            }

        session = RequestHelper.session

        stream = False
        if 'stream' in pars:
            if pars['stream'] == True:
                stream = True

        if 'header' in pars:
            header = pars['header']

        cookies = None
        if 'cookies' in pars:
            cookies = pars['cookies']
        elif 'file_cookie' in pars:
            path = pars['file_cookie']
            cookies = RequestHelper.__read_cookie_from_file(path)

        if RequestHelper.config.proxy_enable:
            req = session.get(url, headers=header, proxies=RequestHelper.proxies, timeout=RequestHelper.config.request_timeout, stream=stream, cookies=cookies)
        else:
            req = session.get(url, headers=header, timeout=RequestHelper.config.request_timeout, stream=stream, cookies=cookies)

        # print(req.cookies)

        return RequestHelper.__to_encode(req, **pars)

    @staticmethod
    def __in_post(url, par, ** pars):
        session = RequestHelper.session

        cookies = None
        if 'cookies' in pars:
            cookies = pars['cookies']
        elif 'file_cookie' in pars:
            path = pars['file_cookie']
            cookies = RequestHelper.__read_cookie_from_file(path)

        json = None
        if 'json' in pars:
            json = pars['json']

        if RequestHelper.config.proxy_enable:
            req = session.post(url, par, proxies=RequestHelper.proxies, timeout=RequestHelper.config.request_timeout, cookies=cookies, json=json)
        else:
            req = session.post(url, par, timeout=RequestHelper.config.request_timeout, cookies=cookies, json=json)

        return RequestHelper.__to_encode(req, **pars)

    @staticmethod
    def __in_post_ex(url, par, ** pars):
        header = None

        if "header" in pars:
            header = pars["header"]

        session = RequestHelper.session

        cookies = None
        if 'cookies' in pars:
            cookies = pars['cookies']
        elif 'file_cookie' in pars:
            path = pars['file_cookie']
            cookies = RequestHelper.__read_cookie_from_file(path)

        if RequestHelper.config.proxy_enable:
            req = session.post(url, par, headers = header, proxies=RequestHelper.proxies, timeout=RequestHelper.config.request_timeout, cookies=cookies)
        else:
            req = session.post(url, par, headers=header, timeout=RequestHelper.config.request_timeout, cookies=cookies)

        return RequestHelper.__to_encode(req, **pars)

    @staticmethod
    def get(url, **pars):
        result = None
        for i in range(0, RequestHelper.config.request_retry_times):
            try:
                result = RequestHelper.__in_get(url, **pars)
                break
            except Exception as e:
                E.out_err(e)

        if result != None:
            return result
        else:
            raise HttpConnectionFailedException(url)

    @staticmethod
    def post(url, par=None, ** pars) -> str:
        result = None
        for i in range(0, RequestHelper.config.request_retry_times):
            try:
                result = RequestHelper.__in_post(url, par, ** pars)

                break
            except Exception as e:
                E.out_err(e)

        if result != None:
            return result
        else:
            raise HttpConnectionFailedException(url)

    @staticmethod
    def post_ex(url, par, ** pars):
        result = None
        for i in range(0, RequestHelper.config.request_retry_times):
            try:
                result = RequestHelper.__in_post_ex(url, par, ** pars)
                break
            except Exception as e:
                pass

        if result != None:
            return result
        else:
            raise HttpConnectionFailedException(url)

    @staticmethod
    def _get(url, **pars):
        result = None
        for i in range(0, RequestHelper.config.request_retry_times):
            try:
                result = RequestHelper.__in__get(url, **pars)
                break
            except Exception as e:
                pass

        if result != None:
            return result
        else:
            raise HttpConnectionFailedException(url)

    @staticmethod
    def __read_cookie_from_file(path:str) -> dict:
        try:
            cookie = file.read(path)
            return string.str_to_dic_ex(cookie, ';', '=')
        except:
            return {}

    # @staticmethod
    # def keep_cookies():
    #     url_login = 'http://www.newrank.cn/xdnphb/login/loginNew'
    #     url_get = 'http://www.newrank.cn/xdnphb/common/account/get'
    #
    #     postdata = urllib.parse.urlencode({
    #         "flag": "14900597112890.41429202715761404",
    #         "identifyCode": "",
    #         "password": "736fdb4cc5ddedce11104d892d94eeb8",
    #         "username": "mediaer_plus@163.com",
    #         "nonce": "2e827b296",
    #         "xyz": "1c5ec61c3930f309785352e0603676af"
    #     }).encode('utf-8')
    #
    #     postdata2 = urllib.parse.urlencode({
    #         "nonce": "40e013483",
    #         "xyz": "ab0d728de9413142c4d29683ab987f17"
    #     }).encode('utf-8')
    #
    #     header = {
    #         "Accept": "application/json, text/javascript, */*; q=0.01",
    #         "Accept-Encoding": "utf-8",
    #         "Accept-Language": "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4",
    #         "Connection": "keep-alive",
    #         "Host": "www.newrank.cn",
    #         "Referer": "http://www.newrank.cn/",
    #         "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0"
    #     }
    #
    #     req = urllib.request.Request(url_login, postdata, header)
    #     req2 = urllib.request.Request(url_get, postdata2, header)
    #     ##print(urllib.request.urlopen(req).read().decode('utf-8'))
    #
    #     # 自动记住cookie
    #     cj = http.cookiejar.CookieJar()
    #     opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
    #     r = opener.open(req)
    #     r2 = opener.open(req2)
    #     print(r.read().decode('utf-8'))
    #     print(r2.read().decode('utf-8'))
    #     print("")
    #
    # @staticmethod
    # def cookies_again():
    #     url_login = 'http://www.newrank.cn'
    #     url_get = 'http://www.newrank.cn/xdnphb/common/account/get'
    #
    #     postdata = urllib.parse.urlencode({
    #         "flag": "14900597112890.41429202715761404",
    #         "identifyCode": "",
    #         "password": "736fdb4cc5ddedce11104d892d94eeb8",
    #         "username": "mediaer_plus@163.com",
    #         "nonce": "2e827b296",
    #         "xyz": "1c5ec61c3930f309785352e0603676af"
    #     }).encode('utf-8')
    #
    #     postdata2 = urllib.parse.urlencode({
    #         "nonce": "40e013483",
    #         "xyz": "ab0d728de9413142c4d29683ab987f17"
    #     }).encode('utf-8')
    #
    #     header = {
    #         "Accept": "application/json, text/javascript, */*; q=0.01",
    #         "Accept-Encoding": "utf-8",
    #         "Accept-Language": "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4",
    #         "Connection": "keep-alive",
    #         "Host": "www.newrank.cn",
    #         "Referer": "http://www.newrank.cn/",
    #         "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0"
    #     }
    #
    #     cookie_filename = 'cookie.txt'
    #     cookie = http.cookiejar.MozillaCookieJar(cookie_filename)
    #     handler = urllib.request.HTTPCookieProcessor(cookie)
    #     opener = urllib.request.build_opener(handler)
    #
    #     request = urllib.request.Request(url_login, headers=header)
    #     try:
    #         response = opener.open(request)
    #         page = response.read().decode()
    #         print(page)
    #     except urllib.error.URLError as e:
    #         print(str(e), ':', e.reason)
    #
    #     cookie.save(ignore_discard=True, ignore_expires=True)  # 保存cookie到cookie.txt中
    #     # print(cookie)
    #     for item in cookie:
    #         print('token = ' + item.token)
    #         print('rmbuser = ' + item.rmbuser)
    #
    #     get_url = url_get  # 利用cookie请求访问另一个网址
    #     get_request = urllib.request.Request(get_url, postdata2, headers=header)
    #     get_response = opener.open(get_request)
    #     print(get_response.read().decode())
    #     print('you are died')
    #
    #
    # @staticmethod
    # def fuck():
    #     url = "http://www.newrank.cn/"
    #     url_login = 'http://www.newrank.cn/xdnphb/login/loginNew'
    #     url_get = 'http://www.newrank.cn/xdnphb/common/account/get'
    #
    #     header = {
    #         "Accept": "application/json, text/javascript, */*; q=0.01",
    #         "Accept-Encoding": "gzip, deflate",
    #         "Accept-Language": "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4",
    #         "Origin": "http://www.newrank.cn",
    #         "X-Requested-With": "XMLHttpRequest",
    #         "Connection": "keep-alive",
    #         "Host": "www.newrank.cn",
    #         "DNT": "1",
    #         "Referer": "http://www.newrank.cn/",
    #         "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
    #     }
    #
    #     par_login = (['flag', '14900597112890.41429202715761404'], ['identifyCode', ''],
    #            ['password', '736fdb4cc5ddedce11104d892d94eeb8'], ['username', 'mediaer_plus@163.com'],
    #            ['nonce', '2e827b296'], ['xyz', '1c5ec61c3930f309785352e0603676af'])
    #
    #     par_get = (['nonce', '40e013483'], ['xyz', 'ab0d728de9413142c4d29683ab987f17'])
    #
    #     session = requests.Session()
    #
    #     req = session.post(url_login, par_login, headers=header)
    #     print(req.text)
    #     req_get = session.get(url,headers=header)
    #     cookies = req_get.cookies
    #     req2 = session.post(url_get, par_get, headers=header)
    #     print(req2.text)
    #     print('')




